const fs = require('fs');
const archiver = require('archiver');
const path = require('path');

// 创建输出目录
const distPath = path.join(__dirname, 'dist');
if (!fs.existsSync(distPath)) {
    fs.mkdirSync(distPath);
}

// 创建一个文件来写入
const output = fs.createWriteStream(path.join(distPath, 'sjs_server.zip'));
const archive = archiver('zip', {
    zlib: { level: 9 } // 设置压缩级别
});

output.on('close', () => {
    console.log('打包完成！');
    console.log('总大小：' + (archive.pointer() / 1024).toFixed(2) + ' KB');
});

archive.on('error', (err) => {
    throw err;
});

// 将输出流管道连接到文件
archive.pipe(output);

// 添加文件到压缩包
const filesToInclude = [
    'app.js',
    'config.js',
    'package.json',
    'router',
    'router_handler',
    'db'
];

// 添加需要的文件和文件夹
filesToInclude.forEach(file => {
    const fullPath = path.join(__dirname, file);
    if (fs.existsSync(fullPath)) {
        if (fs.lstatSync(fullPath).isDirectory()) {
            archive.directory(fullPath, file);
        } else {
            archive.file(fullPath, { name: file });
        }
    }
});

// 完成打包
archive.finalize();